package com.baijiaoxi.common.utils.jdk;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class MathUtil {
    /**
     * 2.1 ->3
     *
     * @param number
     * @return
     */
    public static Float roundUp(Double number, int scale) {
        BigDecimal bigDecimal = new BigDecimal(number);
        BigDecimal result = bigDecimal.setScale(scale, RoundingMode.UP);
        return result.floatValue();
    }

    public static Float round(Float money) {
        DecimalFormat decimalFormat = new DecimalFormat(".00");
        BigDecimal bigDecimal = new BigDecimal(money);
        return Float.valueOf(decimalFormat.format(bigDecimal));
    }

    public static Long parseLong(Object o) {
        if (o == null)
            return null;
        if (o instanceof Long)
            return (Long) o;
        else
            return Long.valueOf(String.valueOf(o));
    }

    public static Long parseLongNullToZero(Object o) {
        Long val = parseLong(o);
        return val == null ? 0 : val;
    }
}
